package cn.jfinalbbs.hnair;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.google.gson.Gson;
import com.jfinal.kit.PropKit;
import com.qq.connect.utils.json.JSONException;
import com.qq.connect.utils.json.JSONObject;

import cn.jfinalbbs.hnair.json.Hnair;
import cn.jfinalbbs.hnair.json.Hnair.Airlowfaresearchrs;
import cn.jfinalbbs.utils.DateUtil;
import cn.jfinalbbs.utils.HttpClientUtil;
import cn.jfinalbbs.utils.MD5;
//对接口进行测试
public class TestMain {
	private String url = "http://114.251.242.135:8080/gxotarest/api/search-low-fare-trips.json";
	private String charset = "utf-8";
	private HttpClientUtil httpClientUtil = null;
	
	public TestMain(){
		httpClientUtil = new HttpClientUtil();
	}
	
	public void test(){
		String httpOrgCreateTest = url;
		String OTACode = "GXAIRLINES_120";//OTACode
		String ip = "220.249.72.237";//签名IP
		String key = "69CDDG7D0042E4C672F46F0BACGAE3H2";//key
		Long timestamp = System.currentTimeMillis();//时间戳
		String leg = "index:0;origin:NNG;originType:A;destination:CSX;destinationType:A;date:2016-03-12";
		String traveler = "code:ADT";
		String cabin = "Economy";
		String flexible = "false";
		String token = OTACode + ip + timestamp + key;
		
		Map<String,String> createMap = new HashMap<String,String>();
		
		appIn(createMap,OTACode,token,timestamp);
		
		searchLowFareTrips(createMap,leg,token,traveler,cabin,flexible);
		
		System.out.println(createMap);
		String httpOrgCreateTestRtn = httpClientUtil.doPost(httpOrgCreateTest,createMap,charset);
		System.out.println(httpOrgCreateTestRtn);
	}
	
	public static StringBuffer read(String file) {
		   String s = null;
		   StringBuffer sb = new StringBuffer();
		   List<String> zhanghaos = new ArrayList<String>();
		   File f = new File(file);
		   if (f.exists()) {
		    try {
		     BufferedReader br = new BufferedReader(new InputStreamReader(
		       new FileInputStream(f)));
		     while ((s = br.readLine()) != null) {
		      sb.append(s);
		      zhanghaos.add(s);
		     }
		     
		    } catch (Exception e) {
		     e.printStackTrace();
		    }
		    return sb;
		   }else{
		    return null;
		   }
		}
	
	/**
	 *  请求头
	  * @author: liuwangpan
	  * @createTime: 2016年3月8日  下午4:49:14
	 */
	public void appIn(Map<String,String> createMap,String OTACode,String token,Long timestamp){
		createMap.put("ln","zh_CN");
		createMap.put("OTACode",OTACode);
		createMap.put("token",MD5.MD5Encode(token));
		createMap.put("timestamp",timestamp.toString());
	}
	
	/**
	  * 查询最低票价
	  * @author: liuwangpan
	  * @createTime: 2016年3月8日  下午4:53:00
	 */
	public void searchLowFareTrips(Map<String,String> createMap,String leg,String token,String traveler,String cabin,String flexible){
		createMap.put("leg", leg);
		createMap.put("traveler", traveler);
		createMap.put("cabin", cabin);
		createMap.put("flexible",flexible);
	}
	
	public static void main(String[] args){
//		TestMain main = new TestMain();
//		main.test();
		
		
		StringBuffer sb = read("C:\\Users\\15816\\git\\jfinalbbs\\src\\main\\resources\\search-low-fare-trips.json");
//		StringBuffer sb = read("C:\\Users\\15816\\git\\jfinalbbs\\src\\main\\resources\\demo.json");
//		JSONObject json;
//		try {
//			json = new JSONObject(sb.toString());
//		    JSONObject airLowFareSearchRS=json.getJSONObject("AirLowFareSearchRS");
//		    String RqCriteria = (String) airLowFareSearchRS.get("@RqCriteria");
//		    
//		    String uri = (String) airLowFareSearchRS.get("@Uri");
//		    System.out.println(uri);
//		    
//		    JSONObject taxFeeContentObj = (JSONObject) airLowFareSearchRS.get("TaxFeeContent");
//		    com.qq.connect.utils.json.JSONArray array = taxFeeContentObj.getJSONArray("TaxFee");
//		    System.out.println(array)
//		    
//		} catch (JSONException e) {
//			e.printStackTrace();
//		}  
		
		JSONObject json;
		try {
			json = new JSONObject(sb.toString());
			Gson gson = new Gson();
			Hnair nair = gson.fromJson(json.toString(), Hnair.class);
			System.out.println(nair.getAirlowfaresearchrs().getUri());
			
			json = new JSONObject(sb.toString());
		    JSONObject airLowFareSearchRS=json.getJSONObject("AirLowFareSearchRS");
		    String uri = (String) airLowFareSearchRS.get("@Uri");
		    System.out.println(uri);
		} catch (JSONException e) {
			e.printStackTrace();
		}  
	    
		
	}
}